package com.tusuo.stds.ui.integrity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelResult;

import java.util.List;

/**
 * Created by developer on 17-9-11.
 */

@SuppressLint("NewApi")
public class IntegrityResultAdapter extends BaseAdapter {

    private List<LevelResult.GroupItem> mData;
    private Context mContext;

    public IntegrityResultAdapter(Context context, List<LevelResult.GroupItem> groupList) {
        mContext = context;
        mData = groupList;
    }


    @Override
    public int getCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;

        if (convertView == null) {
            view = View.inflate(mContext, R.layout.item_integrity_level_result_list,null);
            holder = new ViewHolder();
            holder.layoutBackground = (LinearLayout) view.findViewById(R.id.item_integrity_result_layout_background);
            holder.tvDescriber = (TextView) view.findViewById(R.id.item_integrity_result_tv_describer);
            holder.tvOld = (TextView) view.findViewById(R.id.item_integrity_result_tv_old);
            holder.tvNow = (TextView) view.findViewById(R.id.item_integrity_result_tv_now);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        LevelResult.GroupItem groupItem = mData.get(position);

        switch (compareType(groupItem.OldType,groupItem.Type)) {
            case 1:
                // 合格
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_qualified);
                break;
            case 2:
                // 模糊
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_blurry);
                break;
            case 3:
                // 问题
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_question);
                break;
            case 4:
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_change_qualified);
                break;
            case 5:
                holder.layoutBackground.setBackgroundResource(R.drawable.shape_integrity_change_question);
                break;
            default:
                break;
        }

        holder.tvNow.setText(groupItem.ErrorCount + "");
        holder.tvOld.setText(groupItem.CorrectCount + "");

        holder.tvDescriber.setText(groupItem.Name);
        return view;
    }

    class ViewHolder {
        LinearLayout layoutBackground;
        TextView tvDescriber;
        TextView tvOld;
        TextView tvNow;
    }

    private int compareType(int oldType,int nowType) {

        if (oldType == nowType) {
            // 没变
            return nowType;
        }

        if (nowType == 1) {
            // 变成合格
            return 4;
        }

        if (nowType == 3) {
            // 变成问题
            return 5;
        }

        return 2;
    }
}
